package cn.weiron.fun.infrastructure.utils;

import org.slf4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.time.LocalDateTime;

/**
 * 成都金控数据版权所有
 *
 * @author zhujian
 * @version 1.0
 * @className SendSMSUtils
 * @description   短信发送工具类
 * @email binfenlingxing@hotmail.com
 * @date 2018-01-26
 * @Copyright(C) 2017 by Chengdu Financial Holding
 * Data Services All rights reserved.
 */
public class SendSMSUtils {

    private static  final Logger logger = LoggerUtils.getSMSlog();

    /**
     * 批量发送接口
     */
    private static final String SEND_BATCH_URL = "http://ty.esoftsms.com/ws/BatchSend2.aspx";
    /**
     * 新版接口参数
     */
    private static final String MSG_PARAM = "CorpID=CDYY000086&Pwd=jksj18602829005&Mobile=_PHONE_&Content=_CONTENT_&Cell=&SendTime=&encode=GBK";
    /**
     * 替换手机号
     */
    private static final String PHONE_NUMBER = "_PHONE_";
    /**
     * 替换短信内容
     */
    private static final String MSG_CONTENT = "_CONTENT_";
    /**
     * 短信编码
     */
    private static final String ENCODE = "GBK";

    public static void main(String[] args) {
        SendSMSUtils.sendBatchSMS("18328451591,15390095998","您好，高新区抗疫人员调度信息管理库系统已经开通，请及时关注“健康高新”微信公众号，登录到系统绑定账号，并对个人信息进行确认或更新，谢谢！【成都市高新区教文卫健局】");
    }

    /**
     * 健康成都平台发送短信
     * @param phone   手机号
     * @param content  发送内容
     * @return
     */
    public static Boolean sendSMS(String phone,String content){
        try {
            //编码短信内容
            String msg  = java.net.URLEncoder.encode(content,ENCODE);
            //生成发送参数
            String postData = MSG_PARAM.replace(PHONE_NUMBER,phone.trim()).replace(MSG_CONTENT,msg);
            //记录日志
            logger.info("发送手机号："+phone+",发送内容："+content);
            logger.info("拼装结果："+postData);
            //发送消息
            Long res = sendMsg(SEND_BATCH_URL+"?"+postData);
            logger.info("短信网关响应：{}",res);
            //结果大于等于零则发送成功
            if(res >=0){
                logger.info("短信发送成功");
                return true;
            }
            logger.info("短信发送失败");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            logger.info("不支持的编码！"+e.getMessage());
        }
        return false;
    }
    /**
     * 批量发送短信
     * @param phones  手机号 批量手机号英文逗号隔开
     * @param content  发送内容
     */
    public static void sendBatchSMS(String phones, String content){
        try {
            //编码短信内容
            String msg  = java.net.URLEncoder.encode(content,ENCODE);
            //生成发送参数
            String postData = MSG_PARAM.replace(PHONE_NUMBER,phones.trim()).replace(MSG_CONTENT,msg);
            //记录日志
            logger.info("批量发送手机号："+phones);
            logger.info("发送内容："+content);
            logger.info("拼装结果："+postData);
            //发送消息
            Long res = sendMsg(SEND_BATCH_URL+"?"+postData);
            logger.info("短信网关响应："+res);
            //结果大于等于零则发送成功
            if(res >=0){
                logger.info(LocalDateTime.now()+"短信发送成功!");
                return;
            }
            logger.info("短信发送失败!");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            logger.info("不支持的编码！"+e.getMessage());
        }
    }

    /**
     * 发送短信
     * @param postUrl  发送短信的参数
     * @return 整型  返回大于等于0 都是 提交成功
     */
    public static Long sendMsg(String postUrl) {
        long inputLine;
        try {
            URL url = new URL(postUrl);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            inputLine = new Long(in.readLine());
        } catch (IOException e) {
            inputLine = -2;
            logger.info("网络异常,发送短信失败！"+e.getMessage());
        }
        return inputLine;
    }

}
